package cn.ffcs.uec.organization.component;

import java.util.HashMap;
import java.util.Map;

import org.apache.log4j.Logger;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.zkoss.zk.ui.Components;
import org.zkoss.zk.ui.Executions;
import org.zkoss.zk.ui.IdSpace;
import org.zkoss.zk.ui.SuspendNotAllowedException;
import org.zkoss.zk.ui.event.Event;
import org.zkoss.zk.ui.event.EventListener;
import org.zkoss.zk.ui.event.Events;
import org.zkoss.zul.Div;
import org.zkoss.zul.Treecell;
import org.zkoss.zul.Treechildren;
import org.zkoss.zul.Treeitem;
import org.zkoss.zul.Treerow;
import org.zkoss.zul.Window;

import cn.ffcs.raptornuke.plugin.common.zk.util.ZkUtil;
import cn.ffcs.uec.organization.component.bean.UecPoliticalTreeExtBean;
import cn.ffcs.uec.organization.constant.UecOrganizationConstant;
import cn.ffcs.uec.organization.model.UecOrgDept;
import cn.ffcs.uec.organization.model.UecOrgOrgs;
import cn.ffcs.uec.organization.vo.OrganizationRelationVo;
import cn.ffcs.uom.common.util.IPortletInfoProvider;
import cn.ffcs.uom.common.zkplus.zul.tree.node.TreeNodeEntity;
import cn.ffcs.uom.common.zkplus.zul.tree.node.impl.TreeNodeImpl;
import lombok.Getter;
import lombok.Setter;
@Controller
@Scope("prototype")
public class UecPoliticalTreeExt extends Div implements IdSpace {

	private static final long serialVersionUID = 4903021991325048868L;
	
	private Logger logger = Logger.getLogger(this.getClass());
	/**
	 * .zul
	 */
	private final String zul = "/pages_nmg/uec_organization/comp/uec_political_tree_ext.zul";

	private UecPoliticalTreeExtBean bean = new UecPoliticalTreeExtBean();

	/**
	 * 选中的组织关系
	 */
	private OrganizationRelationVo organizationRelationVo;
	/**
	 * 选中的组织TreeItem
	 */
	@SuppressWarnings("unused")
	private Treeitem selectedTreeitem;

//	private UecOrgOrgsManager uecOrgOrgsManager = (UecOrgOrgsManager) ApplicationContextUtil
//			.getBean("uecOrgOrgsManager");
//	private UecOrgDeptManager uecOrgDeptManager = (UecOrgDeptManager) ApplicationContextUtil
//			.getBean("uecOrgDeptManager");
	/**
	 * 选中的组织/部门 组织与部门分别在两张表中
	 */
	private UecOrgOrgs uecOrgOrgs;
	private UecOrgDept uecOrgDept;
	/**
	 * 
	 */

	@Getter
	@Setter
	private IPortletInfoProvider portletInfoProvider;

	public UecPoliticalTreeExt() {
		Executions.createComponents(this.zul, this, null);
		Components.wireVariables(this, bean);
		Components.addForwards(this, this, '$');

	}

	/**
	 * 增加根节点
	 * 
	 * @throws Exception
	 */
	public void onAddRootNode() throws Exception {
		Treechildren treechildren = this.bean.getUecOrganizationRelationTree()
				.getTreechildren();
		if (null != treechildren && null != treechildren.getChildren()
				&& treechildren.getChildren().size() > 0) {
			ZkUtil.showError("存在根节点,不能添加！", "提示信息");
			return;
		}
		String opType = "addRootNode";
		this.openAddNodeWindow(opType);
	}
	/**
	 * 增加根节点
	 */
	public void onAddRootNodeResponse(
			OrganizationRelationVo organizationRelationVo) {
		Treechildren treechildren = this.bean.getUecOrganizationRelationTree()
				.getTreechildren();
		Treeitem treeitem = new Treeitem();
		Treecell treecell = new Treecell("新增节点");
		Treerow treerow = new Treerow();
		treecell.setParent(treerow);
		treerow.setParent(treeitem);
		TreeNodeImpl<TreeNodeEntity> treeNodeImpl = new TreeNodeImpl<TreeNodeEntity>(
				organizationRelationVo);
		treeitem.setValue(treeNodeImpl);
		treechildren.appendChild(treeitem);
	}

	/**
	 * 增加子节点
	 * 
	 * @throws Exception
	 */
	public void onAddChildNode() throws Exception {
		// 需要添加权限--后续添加

		if (this.bean.getUecOrganizationRelationTree().getSelectedItem() != null) {
			String opType = "addChildNode";
			this.openAddNodeWindow(opType);
		} else {
			ZkUtil.showError("请选择节点", "提示信息");
		}
	}
	/**
	 * 增加子节点
	 */
	public void onAddChildNodeResponse(
			OrganizationRelationVo organizationRelationVo) {
		Treechildren treechildren = this.bean.getUecOrganizationRelationTree()
				.getSelectedItem().getTreechildren();
		// 如果没有下级
		if (null != treechildren) {
			/**
			 * 父节点设置下级孩子为null让其查库，避免增加了节点不展示的问题
			 */
			TreeNodeImpl<?> parentTreeNodeImpl = (TreeNodeImpl<?>) this.bean
					.getUecOrganizationRelationTree().getSelectedItem()
					.getValue();
			parentTreeNodeImpl.setChildren(null);
			this.bean.getUecOrganizationRelationTree().getSelectedItem()
					.setValue(parentTreeNodeImpl);
			Treechildren tchild = new Treechildren();
			Treeitem titem = new Treeitem();
			Treerow trow = new Treerow();
			Treecell tcell = new Treecell(organizationRelationVo.getLabel());
			/**
			 * 单位label样式
			 */
			if ("DEPT".equals(organizationRelationVo.getOrgType())) {
				tcell.setStyle("color:blue");
			}
			tcell.setParent(trow);
			trow.setParent(titem);
			TreeNodeImpl<TreeNodeEntity> treeNodeImpl = new TreeNodeImpl<TreeNodeEntity>(
					organizationRelationVo);
			titem.setValue(treeNodeImpl);
			titem.setParent(tchild);
			tchild.setParent(this.bean.getUecOrganizationRelationTree()
					.getSelectedItem());
		} else {
			// 如果已经存在下一级
			Treeitem titem = new Treeitem();
			Treerow trow = new Treerow();
			Treecell tcell = new Treecell(organizationRelationVo.getLabel());
			/**
			 * 单位类label样式
			 */
			if ("DEPT".equals(organizationRelationVo.getOrgType())) {
				tcell.setStyle("color:blue");
			}
			tcell.setParent(trow);
			trow.setParent(titem);
			TreeNodeImpl<TreeNodeEntity> treeNodeImpl = new TreeNodeImpl<TreeNodeEntity>(
					organizationRelationVo);
			titem.setValue(treeNodeImpl);
			titem.setParent(treechildren);
		}
	}

	/**
	 * 删除节点
	 * 
	 * @throws Exception
	 */
	public void onDelNode() throws Exception {
		if (null != this.bean.getUecOrganizationRelationTree()
				.getSelectedItem()) {
			Treechildren treechildren = this.bean
					.getUecOrganizationRelationTree().getSelectedItem()
					.getTreechildren();
			if (null != treechildren) {
				ZkUtil.showError("存在下级节点,不能删除", "提示信息");
				return;
			}
			Window window = (Window) Executions.createComponents(
					"/pages/common/del_reason_input.zul", this, null);
			window.doModal();
			window.addEventListener(Events.ON_OK, new EventListener() {

				@Override
				public void onEvent(Event event) throws Exception {
					if (event.getData() != null) {

					}
				}
			});
		} else {
			ZkUtil.showError("请选择你要删除的节点", "提示信息");
		}
	}
	/**
	 * 点击时选择树
	 */
	public void onSelect$uecOrganizationRelationTree() {
		selectedTreeitem = this.bean.getUecOrganizationRelationTree()
				.getSelectedItem();
		organizationRelationVo = (OrganizationRelationVo) ((TreeNodeImpl<?>) this.bean
				.getUecOrganizationRelationTree().getSelectedItem().getValue())
				.getEntity();
		logger.info("全称："+organizationRelationVo.getFullName());
		if (null != organizationRelationVo
				&& null != organizationRelationVo.getOrg()) {
			/**
			 * 根据orgType来判断所选的是部门还是组织 组织类型 orgType = org; --组织 orgType =
			 * dept;--部门
			 */
//			if ("ORG".equals(organizationRelationVo.getOrgType())) {
//				uecOrgOrgs = uecOrgOrgsManager
//						.findUecOrgOrgsByOrganizationRelationVo(organizationRelationVo);
//
//			}
//			if ("DEPT".equals(organizationRelationVo.getOrgType())) {
//				uecOrgDept = uecOrgDeptManager
//						.findUecOrgDeptByOrganizationRelationVo(organizationRelationVo);
//			}
			Events.postEvent(
					UecOrganizationConstant.ON_SELECT_ORGREATION_TREE_REQUEST,
					this, organizationRelationVo);
		}
	}

	/**
	 * 相关界面
	 * 
	 * @throws Exception
	 * @throws SuspendNotAllowedException
	 */
	public void openAddNodeWindow(String opType)
			throws SuspendNotAllowedException, Exception {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("opType", opType);
		map.put("uecOrgOrgs", uecOrgOrgs);
		map.put("uecOrgDept", uecOrgDept);
		Window win = (Window) Executions.createComponents(
				"/pages/organization/organization_tree_node_edit.zul", this,
				map);
		win.doModal();
		win.addEventListener("onOK", new EventListener() {
			@Override
			public void onEvent(Event event) throws Exception {

			}

		});
	}

}
